package com.vrv.netmgr.alarm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.vrv.common.Pagination;
import com.vrv.common.UrlPrefix;
import com.vrv.common.util.HttpClientToServer;
import com.vrv.common.util.JsonUtil;
import com.vrv.netmgr.R;
import com.vrv.netmgr.assets.detail.DetailAssetsQryActivityDemo;
import com.vrv.netmgr.vo.ErrorMessage;

/**
 * 注册资产查询demo
 * @author zxb
 *
 */
public class EquipmentChangedQryActivityDemo extends Activity{
    private static final String TAG = EquipmentChangedQryActivityDemo.class.getSimpleName();
    private static final boolean DEBUG = true;
    
    /**分页对象*/
    public Pagination pagination;
    /**基本查询条件*/
    private EditText et_condition;
    /**基本查询条件封装*/
    private Map<String, String> mNormalQueryCondition;
    /**高级查询条件*/
    private Map<String, String> mAdvancedQueryCondition;
   
    /**等待进度条*/
    private ProgressDialog progressDialog;
    /**查询按钮*/
    private ImageButton imgbtn_search;
    /**标题*/
    private TextView tv_title;
    /**返回按钮*/
    private ImageButton imgbtn_back;
    /**数据列表*/
    private ListView listView;
    /**列表的页脚视图*/
    private View loadMoreView;
    private Button loadNextButton;
    private Button loadPreButton;
    
    /**适配器*/
    private EquipmentChangedAdapter equipmentChangedAdapter;
    /**适配器数据集合*/
    public List<ErrorMessage> mErrorMessages;
    
    /**url链接*/
    private String mTodayUrl;
    private String mBygoneUrl;
    /**缓存链接地址*/
    private String mTempUrl;
    
    /**查询条件ip键*/
    private static final String KEY_IPADDRES = "ipaddress1";
//	/**注册类型*/
//	private static final String KEY_REGISTERED_TYPE = "registered";
//	/**未注册*/
//	private static final String REGISTERED_TYPE_0 = "0";
//	/**已注册*/
//	private static final String REGISTERED_TYPE_1 = "1";

    
    /**查询成功*/
    private static final int QUERY = 0x100;
    /**查询失败*/
    private static final int ERROR = 0x101;
    /**没有数据*/
    private static final int NULL = 0x102;
    
    /**查询方式*/
    private static final int TYPE_0 = 0;
    private static final int TYPE_1 = 1;
    private static final int TYPE_2 = 2;
    private int mTempType = TYPE_0;

    /**PopupWindow相关操作控件*/
    private ImageButton moreFuncBtn;
    private PopupWindow pwMyPopWindow;
    private ListView lvPopupList;
    private ArrayAdapter<String> mArrayAdapter;
    
    @Override
    public void onBackPressed() {
        if(DEBUG) Log.v(TAG, "onBackPressed()");
        
        overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
        super.onBackPressed();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        if(DEBUG) Log.v(TAG, "onCreate()");
        
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.assets_list_page);
//		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_title);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tools_title);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        
        init();
        bindUI();
        bindListener();
        iniPopupWindow();
        
        autoQuery();
    }

    /**
     * 开启查询线程
     */
    public void runQueryThread() {
        if (DEBUG) Log.v(TAG, "runQueryThread()");

        new Thread(new Runnable() {
            @Override
            public void run() {
                if (null == mErrorMessages) {
                    mErrorMessages = new ArrayList<ErrorMessage>();
                } else {
                    mErrorMessages.clear();
                }

                String response = query(mTempUrl);
                Log.v(TAG, "runQueryThread()...response = " + response);

                ErrorMessageResponse deviceResponse = analysis(response);
                if (null == deviceResponse) {
                    handler.sendEmptyMessage(NULL);
                    return;
                }
                
                updateTotalRecords(deviceResponse);
                
                mErrorMessages = deviceResponse.getErrorMessages();
                Log.v(TAG, "runQueryThread()...mErrorMessages.size = " + mErrorMessages.size());

                if (null != mErrorMessages || mErrorMessages.size() > 0) {
                    handler.sendEmptyMessage(QUERY);
                }else {
                    handler.sendEmptyMessage(ERROR);
                }
            }
        }).start();
    }
    
    /**查询结果为空*/
    public void clearUI(){
        if (DEBUG) Log.v(TAG, "clearUI()");
        
        if (null != progressDialog && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        
        if (listView.getFooterViewsCount() > 0) {
            listView.removeFooterView(loadMoreView);
            listView.setVisibility(View.INVISIBLE);
        }
        
        if (null != mErrorMessages && mErrorMessages.size() > 0) {
            mErrorMessages.clear();
            equipmentChangedAdapter.notifyDataSetChanged();
        }
    }
    
    /**
     * 封装查询参数
     */
    public void wrapQueryCondition(String condition){
        if (DEBUG) Log.v(TAG, "wrapQueryCondition()");
        
        if (DEBUG) Log.v(TAG, "wrapQueryCondition()...mTempType = " + mTempType);
        
        if (mTempType == TYPE_0 || mTempType == TYPE_1) {
            if (null == mNormalQueryCondition) {
                mNormalQueryCondition = new HashMap<String, String>();
            }
            /**一般查询以IP地址*/
            String ip = et_condition.getText().toString();
            mNormalQueryCondition.put(KEY_IPADDRES, ip);
        }else if (mTempType == TYPE_2){
            if (null == mAdvancedQueryCondition) {
                mAdvancedQueryCondition = new HashMap<String, String>();
            }
            if (null == condition || "".equals(condition)) {
                return;
            }
            
        }
    }
    
    /**
     * 根据查询类型初始化相关内容
     * 初始化链接参数
     * 初始化查询条件
     * 初始化其他参数
     */
    public void initQueryType(){
        if (DEBUG) Log.v(TAG, "initQueryType()");
        
        /**链接*/
        if(mTempType == TYPE_0){
            mTempUrl = mTodayUrl;
        }else if (mTempType == TYPE_1) {
            mTempUrl = mBygoneUrl;
        }
        
        /**和类型相关参数*/
        if(mTempType == TYPE_0){

        }else if (mTempType == TYPE_1) {
            
        }else if(mTempType == TYPE_2){
            
        }
        
        /**查询条件*/
        if(mTempType == TYPE_0 || mTempType == TYPE_1){
            mAdvancedQueryCondition = null;
        }else if(mTempType == TYPE_2){
            mNormalQueryCondition = null;
        }
        
    }
    
    
    /**
     * 自动查询操作
     */
    public void autoQuery(){
        if(DEBUG) Log.v(TAG, "autoQuery()");
        
        mTempType = TYPE_0;
        
        showWaitDialog();
        wrapQueryCondition(null);
        initQueryType();
        initPagingParameter();
        runQueryThread();
    }
    
    /**
     * 启动查询等待
     */
    public void showWaitDialog() {
        if(DEBUG)Log.v(TAG, "showWaitDialog()");
        
        if (null != progressDialog && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        progressDialog = ProgressDialog.show(EquipmentChangedQryActivityDemo.this, "", "查询中...", true, true);
    }
    
    /** 消息处理 */
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if(DEBUG) Log.v(TAG, "handler.handleMessage()");
            
            switch (msg.what) {
            case QUERY:
                progressDialog.dismiss();
                updatePaging();
                updateList();
                break;
            case ERROR:
                progressDialog.dismiss();
                Toast.makeText(EquipmentChangedQryActivityDemo.this, "网络连接失败", Toast.LENGTH_LONG).show();
                break;
            case NULL:
                clearUI();
                Toast.makeText(EquipmentChangedQryActivityDemo.this, "无相关数据", Toast.LENGTH_LONG).show();
            }
        }
    };
    
    /**
     * 获取查询参数,将所有查询条件合并到map
     * @param advanced 高级查询条件
     * @return 返回所有条件map集合
     */
    public Map<String, String> getQueryParameter(){
        if(DEBUG) Log.v(TAG, "getQueryParameter()");
        
        Map<String, String> map = new HashMap<String, String>();
        
        /**分页参数*/
        map.put(Pagination.KEY_PAGE_NUMBER, "" + pagination.pageNumber);
        map.put(Pagination.KEY_PAGE_SIZE, "" + pagination.pageSize);
        
        /**业务参数*/
        if (null != mAdvancedQueryCondition && mAdvancedQueryCondition.size() > 0) {
            /**高级查询*/
            Set<String> keys = mAdvancedQueryCondition.keySet();
            for (Iterator<String> it = keys.iterator(); it.hasNext();) {
                String key = (String) it.next();
                String value = mAdvancedQueryCondition.get(key);
                
                map.put(key, value);
            }
        }else {
            /**一般查询以IP地址*/
            Set<String> keys = mNormalQueryCondition.keySet();
            for (Iterator<String> it = keys.iterator(); it.hasNext();) {
                String key = (String) it.next();
                String value = mNormalQueryCondition.get(key);
                
                map.put(key, value);
            }
        }
        
        return map;
    }
    
    /**
     * 初始化查询参数
     */
    public void initPagingParameter(){
        if(DEBUG) Log.v(TAG, "initQueryParameter()");
        
        if (null == pagination) {
            pagination = new Pagination();
        }
        
        /**this is test*/
        pagination.pageNumber = 0;
        pagination.totalRecords = 0;
        pagination.pageSize = 20;
    }
    
    /**
     * 更新记录总数和分页查询相关
     * @param response 服务器返回的数据结构体
     */
    public void updateTotalRecords(ErrorMessageResponse response){
        if(DEBUG) Log.v(TAG, "updatePagingParameter()");
        
        if (null == response) {
            return;
        }
        
        pagination.totalRecords = response.getTotalRecords();
    }
    
    /**
     * 更新查询页号
     * @param paging翻页标记true表示下一页，false表示上一页
     */
    public void updatePageNumber(boolean paging){
        if(DEBUG) Log.v(TAG, "updateQueryParameter()");
        
        if (paging) {
            pagination.pageNumber += 1;
        }else {
            pagination.pageNumber -= 1;
        }
    }
    
    /**
     * 查询数据
     * @param url 模块链接
     * @param advanced 高级查询条件
     * @return 以字符串返回服务器查询结果
     */
    public String query(String url){
        if(DEBUG) Log.v(TAG, "query()");
        
        String ret = "";
        
        HttpClientToServer httpClientToServer = new HttpClientToServer();
        
        Map<String, String> map = getQueryParameter();
        if(DEBUG) Log.v(TAG, "query()...map = " + map);
        
        if(DEBUG) Log.v(TAG, "query()...url = " + url);
        
        String response = httpClientToServer.doPost(url, map);
        if (!HttpClientToServer.DO_POST_ERR.equals(response) && !HttpClientToServer.DO_POST_EXCEPT.equals(response)) {
            ret = response;
        }
        
        if(DEBUG) Log.v(TAG, "query()...response = " + response);
        
        return ret;
    }
    
    /**
     * 更新分页控件显示
     */
    public void updatePaging(){
        if(DEBUG) Log.v(TAG, "updatePaging()");

        if (listView.getFooterViewsCount() > 0) {
            listView.removeFooterView(loadMoreView);
        }
        
        loadNextButton.setText(R.string.seeNextPage);
        loadPreButton.setText(R.string.seePrePage);
        
        if (pagination.totalRecords <= pagination.pageSize) {
            /**不显示分页控件*/
            loadNextButton.setVisibility(View.GONE);
            loadPreButton.setVisibility(View.GONE);
            
            if(DEBUG) Log.v(TAG, "updatePaging()...不显示分页控件");
        }else if (pagination.totalRecords > pagination.pageSize && 0 == pagination.pageNumber && true) {
            /**显示下一页*/
            loadNextButton.setVisibility(View.VISIBLE);
            loadPreButton.setVisibility(View.GONE);
            
            if(DEBUG) Log.v(TAG, "updatePaging()...显示下一页");
        }else if (pagination.totalRecords > pagination.pageSize && pagination.pageNumber > 0 && ((pagination.pageNumber + 1) * pagination.pageSize < pagination.totalRecords) && true) {
            /**显示上一页、下一页*/
            loadNextButton.setVisibility(View.VISIBLE);
            loadPreButton.setVisibility(View.VISIBLE);
            
            if(DEBUG) Log.v(TAG, "updatePaging()...显示上一页、下一页");
        }else if (pagination.totalRecords > pagination.pageSize && pagination.pageNumber > 0 && ((pagination.pageNumber + 1) * pagination.pageSize >= pagination.totalRecords) && true) {
            /**显示上一页*/
            loadNextButton.setVisibility(View.GONE);
            loadPreButton.setVisibility(View.VISIBLE);
            
            if(DEBUG) Log.v(TAG, "updatePaging()...显示上一页");
        }
        
        listView.addFooterView(loadMoreView);
    }
    
    
    /**
     * 解析服务器返回的字符串
     * @param response 服务器返回的字符串
     * @return 返回指定的数据结构体
     */
    public ErrorMessageResponse analysis(String response){
        if(DEBUG) Log.v(TAG, "analysis()");
        
        ErrorMessageResponse deviceResponse = new ErrorMessageResponse();
        
        List<ErrorMessage> errorMessages = null;
        
        if (null == response || "".equals(response)) {
            return null;
        }

        try {
            JSONTokener jsonParser = new JSONTokener(response);
            JSONObject jsonObj = (JSONObject) jsonParser.nextValue();
            
            boolean b = jsonObj.getBoolean("success");
            deviceResponse.setSuccess(b);
            if (!b) {
                return null;
            }
            
            int count = jsonObj.getInt("totalRecords");
            deviceResponse.setTotalRecords(count);
            if (count <= 0) {
                return null;
            }
            
            JSONArray jArr = jsonObj.getJSONArray("records");
            deviceResponse.setRecords(jArr.length());
            if (null == jArr || jArr.length() == 0) {
                return null;
            }
            
            errorMessages = new ArrayList<ErrorMessage>();
            
            int len = jArr.length();
            for (int i = 0; i < len; i++) {
                JSONObject jObj = (JSONObject) jArr.opt(i);
                
                ErrorMessage deviceInfo = new ErrorMessage();
                
                deviceInfo.setDeptName(JsonUtil.getJsonString(jObj, "deptName"));
                deviceInfo.setOfficeName(JsonUtil.getJsonString(jObj, "officeName"));
                deviceInfo.setUserName(JsonUtil.getJsonString(jObj, "userName"));
                deviceInfo.setTel(JsonUtil.getJsonString(jObj, "tel"));
                deviceInfo.setIpaddress(JsonUtil.getJsonString(jObj, "ipaddress"));
                deviceInfo.setDeviceName(JsonUtil.getJsonString(jObj, "deviceName"));
                deviceInfo.setIpaddress1(JsonUtil.getJsonString(jObj, "ipaddress1"));
                deviceInfo.setErrorMsg(JsonUtil.getJsonString(jObj, "errorMsg"));
                deviceInfo.setSysUserName(JsonUtil.getJsonString(jObj, "sysUserName"));
//				deviceInfo.setErrorTime(JsonUtil.getJsonString(jObj, "errorTime"));
                
                errorMessages.add(deviceInfo);
            }
            
            deviceResponse.setErrorMessages(errorMessages);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        return deviceResponse;
    }
    
    /**
     * 更新UI列表
     */
    public void updateList(){
        if(DEBUG) Log.v(TAG, "updateList()");
        
        if (null == equipmentChangedAdapter) {
            equipmentChangedAdapter = new EquipmentChangedAdapter(mErrorMessages, EquipmentChangedQryActivityDemo.this);
        }else {
            equipmentChangedAdapter.mErrorMessages = mErrorMessages;
        }
        
        listView.setVisibility(View.VISIBLE);
        listView.setAdapter(equipmentChangedAdapter);
        
        equipmentChangedAdapter.notifyDataSetChanged();
    }
    
    /**
     * LISTVIEW适配器继承BaseAdapter
     * @author zxb
     *
     */
    class EquipmentChangedAdapter extends BaseAdapter {

        public Context mContext;
        public List<ErrorMessage> mErrorMessages;

        public EquipmentChangedAdapter(List<ErrorMessage> list, Context context) {
            this.mErrorMessages = list;
            this.mContext = context;
        }

        @Override
        public int getCount() {
            return mErrorMessages.size();
        }

        @Override
        public Object getItem(int position) {
            return mErrorMessages.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = LayoutInflater.from(EquipmentChangedQryActivityDemo.this).inflate(R.layout.equipmentchange_listpage_item, null);
            }
            
            TextView ipaddres = (TextView) convertView.findViewById(R.id.eqmc_ipaddres);
            TextView deviceName = (TextView) convertView.findViewById(R.id.eqmc_deviceName);
            TextView errorMsg = (TextView) convertView.findViewById(R.id.eqmc_errorMsg);
            
            ipaddres.setText(mErrorMessages.get(position).getIpaddress1());
            deviceName.setText(mErrorMessages.get(position).getDeviceName());
            errorMsg.setText(mErrorMessages.get(position).getErrorMsg());
            
            return convertView;
        }

    }
    
    /**
     * LISTVIEW列表事件继承OnItemClickListener
     * @author zxb
     *
     */
    private class OnItemClick implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            ErrorMessage deviceInfo = mErrorMessages.get(arg2);
            
            Intent intent = new Intent(EquipmentChangedQryActivityDemo.this, DetailAssetsQryActivityDemo.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("info", deviceInfo);
            intent.putExtras(bundle);
            startActivity(intent);
        }
        
    }
    
    /**
     * 查询按钮事件
     * @author zxb
     *
     */
    private class SearchOnBtnClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            if(DEBUG) Log.v(TAG, "SearchOnBtnClick.onClick()");
            
            showWaitDialog();
            wrapQueryCondition(null);
            initQueryType();
            initPagingParameter();
            runQueryThread();
        }
        
    }
    

    /**
     * 初始化popupwindow
     */
    private void iniPopupWindow() {
        if(DEBUG) Log.v(TAG, "iniPopupWindow()");
        
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.title_func_popupwindow, null);
        
        /**list选项*/
        lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
        
        pwMyPopWindow = new PopupWindow(layout);
        // 加上这个popupwindow中的ListView才可以接收点击事件
        pwMyPopWindow.setFocusable(true);
        
        /**list选项数据*/
        mArrayAdapter = new ArrayAdapter<String>(
                                            EquipmentChangedQryActivityDemo.this,
                                            R.layout.list_item_popupwindow,
                                            R.id.tv_list_item,
                                            getResources().getStringArray(
                                            R.array.EquipmentChanged));
        
        lvPopupList.setAdapter(mArrayAdapter);
        
        lvPopupList.setOnItemClickListener(new PopupWindowOnItemClickListener());
        
        // 控制popupwindow的宽度和高度自适应
        lvPopupList.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
        // pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight()) * 3);
        pwMyPopWindow.setHeight(LayoutParams.WRAP_CONTENT);

        // 控制popupwindow点击屏幕其他地方消失
        pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg_popupwindow));
        // 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上
        pwMyPopWindow.setOutsideTouchable(true);
    }
    
    /**
     * 初始化popupwindow事件
     */
    private class PopupWindowOnItemClickListener implements AdapterView.OnItemClickListener{
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                final int position, long id) {
            if(DEBUG) Log.v(TAG, "PopupWindowOnItemClickListener.onItemClick()");
            
            if(DEBUG) Log.v(TAG, "PopupWindowOnItemClickListener.onItemClick()...position = " + position) ;
            
            pwMyPopWindow.dismiss();
            /**根据查询方式查询数据*/
            if (0 == position) {
                mTempType = TYPE_0;
                
                showWaitDialog();
                wrapQueryCondition(null);
                initQueryType();
                initPagingParameter();
                runQueryThread();
            }else if (1 == position) {
                mTempType = TYPE_1;
                
                showWaitDialog();
                wrapQueryCondition(null);
                initQueryType();
                initPagingParameter();
                runQueryThread();
            }else if (2 == position) {
                /**高级查询*/
            }
            
//			/**提示用户目前查询方式*/
//			Toast.makeText(AssetsQryActivityDemo.this, mArrayAdapter.getItem(position).toString(), Toast.LENGTH_LONG).show();
        }
    }

    /**
     *  返回数据解析结果
     * @author zxb
     *
     */
    class ErrorMessageResponse{
        public boolean success;
        public int totalRecords;
        public int records;
        List<ErrorMessage> errorMessages;
        
        public boolean isSuccess() {
            return success;
        }
        public void setSuccess(boolean success) {
            this.success = success;
        }
        public int getTotalRecords() {
            return totalRecords;
        }
        public void setTotalRecords(int totalRecords) {
            this.totalRecords = totalRecords;
        }
        public int getRecords() {
            return records;
        }
        public void setRecords(int records) {
            this.records = records;
        }
        public List<ErrorMessage> getErrorMessages() {
            return errorMessages;
        }
        public void setErrorMessages(List<ErrorMessage> errorMessages) {
            this.errorMessages = errorMessages;
        }
        
    }
    
    /**
     * 数据初始化
     */
    private void init(){
        if(DEBUG) Log.v(TAG, "init()");
    
        initUrl();
    }
    
    /**
     * 链接初始化
     */
    private void initUrl(){
        if(DEBUG) Log.v(TAG, "initUrl()");
        
        UrlPrefix urlPrefix = new UrlPrefix(this);
        String prefix = urlPrefix.getUrlPrefix();
        mTodayUrl = prefix + "/" + "errorMessage" + "/" + "jsonList.do?errorType=1&today=true";
        mBygoneUrl = prefix + "/" + "errorMessage" + "/" + "jsonList.do?errorType=1&today=false";
    }
    
    /**
     * 初始化UI
     */
    public void bindUI(){
        if(DEBUG) Log.v(TAG, "bindUI()");
        
        tv_title = (TextView) findViewById(R.id.title_text);
        tv_title.setText("设备变化");
        //
        listView = (ListView) findViewById(R.id.assets_lv_id);
        //
        imgbtn_back = (ImageButton) findViewById(R.id.backBtn);
        imgbtn_back.setVisibility(View.VISIBLE);
        //
        et_condition = (EditText)findViewById(R.id.assets_conditionQry);
        imgbtn_search = (ImageButton)findViewById(R.id.search_go_btn);
        //
        loadMoreView = getLayoutInflater().inflate(R.layout.list_page_load, null);
        loadNextButton = (Button) loadMoreView.findViewById(R.id.loadNextButton);
        loadPreButton = (Button) loadMoreView.findViewById(R.id.loadPreButton);
        /**更多操作按钮*/
        moreFuncBtn = (ImageButton) findViewById(R.id.moreFunctionBtn);
    }
    
    /**
     * 绑定监听事件
     */
    public void bindListener() {
        if(DEBUG) Log.v(TAG, "bindListener()");
        
        imgbtn_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        //
        listView.setOnItemClickListener(new OnItemClick());
        //
        imgbtn_search.setOnClickListener(new SearchOnBtnClick());
        //
        loadNextButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                loadNextButton.setText(R.string.waitingPage);
                
                updatePageNumber(true);
                
                runQueryThread();
            }
        });
        //
        loadPreButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                loadPreButton.setText(R.string.waitingPage);
                
                updatePageNumber(false);
                
                runQueryThread();
            }
        });
        /**更多操作按钮*/
        moreFuncBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pwMyPopWindow.isShowing()) {
                    pwMyPopWindow.dismiss();// 关闭
                } else {
                    pwMyPopWindow.showAsDropDown(moreFuncBtn);// 显示
                }
            }
        });
    }

}
